package com.example.demo.controller;

import com.example.demo.core.Result;
import com.example.demo.domain.security.Token;
import com.example.demo.entity.AdminPermission;
import com.example.demo.entity.AdminRole;
import com.example.demo.entity.AdminUser;
import com.example.demo.exception.BadRequestException;
import com.example.demo.pojo.request.CreateAdminUserRequest;
import com.example.demo.pojo.request.LoginAdminUserRequest;
import com.example.demo.service.AdminUserService;
import com.example.demo.unitls.IpUnits;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 管理员用户控制器
 *
 * @author 叮当猫的百宝箱
 * @since 1.0
 */
@Tag(name = "管理员用户管理", description = "管理员用户相关功能")
@Slf4j
@RestController
@RequestMapping("/adminUser")
@RequiredArgsConstructor
public class AdminUserController {

    private final AdminUserService adminUserService;

    /**
     * 获取全部的管理员对象
     *
     * @return API全局响应对象
     * @deprecated
     */
    @Operation(summary = "获取管理员列表")
    @GetMapping()
    public Result<List<AdminUser>> getAdminUserList() {
        List<AdminUser> adminUserList = adminUserService.getAllAdminUser();
        return Result.ok(adminUserList);
    }

    /**
     * 根据管理员ID获取管理员拥有哪些角色
     *
     * @param adminUserId 管理员用户ID
     * @return API全局响应对象
     */
    @Operation(summary = "获取管理员角色信息")
    @GetMapping("/{adminUserId}/role")
    public Result<List<AdminRole>> getAdminRoleByAdminUserId(@PathVariable("adminUserId") Long adminUserId) {
        List<AdminRole> roleList = adminUserService.getAdminRolesById(adminUserId);
        return Result.ok(roleList);
    }

    /**
     * 根据管理员ID获取管理员拥有哪些权限
     *
     * @param adminUserId 管理员用户ID
     * @return API全局响应对象
     */
    @Operation(summary = "获取管理员权限信息")
    @GetMapping("{adminUserId}/permission")
    public Result<List<AdminPermission>> getAdminPermissionByAdminUserId(@PathVariable("adminUserId") Long adminUserId) {
        List<AdminPermission> roleList = adminUserService.getAdminPermissionsById(adminUserId);
        return Result.ok(roleList);
    }

    /**
     * 设置管理员角色
     *
     * @param adminUserId 管理员用户ID
     * @param roleIds     要设置的管理员角色ID集合
     * @return API全局响应对象
     */
    @Operation(summary = "更新管理员角色")
    @PostMapping("{adminUserId}/adminUserRole")
    public Result<Void> updateAdminUserRoleById(@PathVariable("adminUserId") Long adminUserId, @RequestBody List<Long> roleIds) {
        adminUserService.updateAdminUserRoles(adminUserId, roleIds);
        return Result.ok();
    }

    /**
     * 添加一个管理员用户
     *
     * @param createAdminUser 待创建的管理员用户
     * @return API全局响应对象
     */
    @Operation(summary = "添加一个管理员用户")
    @PostMapping("/create")
    @PreAuthorize("hasAuthority('/adminUser/create')")
    public Result<Void> createAdminUser(@RequestBody @Validated CreateAdminUserRequest createAdminUser, HttpServletRequest request) {
        // 检查密码和二次确认密码是否一致
        if (!createAdminUser.getPassword().equals(createAdminUser.getConfirmPassword())) {
            throw new BadRequestException("确认密码不正确");
        }
        AdminUser adminUser = new AdminUser();
        adminUser.setUsername(createAdminUser.getUsername());
        adminUser.setPassword(createAdminUser.getPassword());

        String clientIp = IpUnits.getClientIp(request);
        adminUserService.createAdminUser(adminUser, clientIp);
        return Result.ok();
    }


    /**
     * 管理员登陆(通过管理员用户名和密码得到Token)
     *
     * @param loginAdminUser 管理员登陆信息
     * @return API全局响应对象
     */
    @Operation(summary = "通过用户名密码获取token")
    @PostMapping("/login")
    public Result<Token> getToken(@RequestBody @Validated LoginAdminUserRequest loginAdminUser, HttpServletRequest request) {
        String clientIp = IpUnits.getClientIp(request);
        Token token = adminUserService.getToken(loginAdminUser.getUsername(), loginAdminUser.getPassword(), clientIp);
        return Result.ok(token);
    }

    /**
     * 删除指定ID的管理员用户
     *
     * @param adminUserId 管理员用户ID
     * @return API全局响应对象
     */
    @Operation(summary = "删除管理员用户")
    @PostMapping("/{adminUserId}/delete")
    @PreAuthorize("hasAuthority('/adminUser/delete')")
    public Result<Void> deleteAdminUserById(@PathVariable("adminUserId") Long adminUserId) {
        adminUserService.deleteAdminUserById(adminUserId);
        return Result.ok();
    }


    /**
     * 刷新管理员用户认证信息
     *
     * @return API全局响应对象
     */
    @Operation(summary = "刷新管理员用户认证信息")
    @PostMapping("/refreshToken")
    public Result<Token> refreshToken() {
        Token token = adminUserService.refreshToken();
        return Result.ok(token);
    }

}
